package cd.tools_test.usbhelper.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cd.tools_test.R;

public class DeviceAdapter extends BaseAdapter {

    private Context context;
    private List<Item> itemList;

    public DeviceAdapter(Context context) {
        this.context = context;
        itemList = new ArrayList<Item>();
    }

    public void addDevice(Item item) {
        removeDevice(item);
        itemList.add(item);
    }

    public void removeDevice(Item item) {
    	itemList.remove(item);
    }

//    public void clearConnectedDevice() {
//        for (int i = 0; i < itemList.size(); i++) {
//            Item device = itemList.get(i);
//            if (BleManager.getInstance().isConnected(device)) {
//                itemList.remove(i);
//            }
//        }
//    }
//
//    public void clearScanDevice() {
//        for (int i = 0; i < itemList.size(); i++) {
//            Item device = itemList.get(i);
//            if (!BleManager.getInstance().isConnected(device)) {
//                itemList.remove(i);
//            }
//        }
//    }

    public void clear() {
    	itemList.clear();
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Item getItem(int position) {
        if (position > itemList.size())
            return null;
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.adapter_devices, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            holder.img_blue = (ImageView) convertView.findViewById(R.id.img_blue);
            holder.txt_name = (TextView) convertView.findViewById(R.id.txt_name);
            holder.txt_mac = (TextView) convertView.findViewById(R.id.txt_mac);
            holder.tv_connectstatus = (TextView) convertView.findViewById(R.id.tv_connectstatus);
            holder.btn_connect_operation = (Button) convertView.findViewById(R.id.btn_connect_operation);
            holder.btn_detail = (Button) convertView.findViewById(R.id.btn_detail);
        }

        final Item item = getItem(position);
        if (item != null) {
            String name = item.getName();
            String mac = item.getDescription();
            holder.txt_name.setText(name);
            holder.txt_mac.setText(mac);
        }

        

        holder.btn_connect_operation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	
            	if(holder.btn_connect_operation.getText().equals(context.getString(R.string.connect))){
            		
            		if (mListener != null) {
                        mListener.onConnect(item);
                    }
            		
            	}else{
            		if (mListener != null) {
                        mListener.onDisConnect(item);
                    }
            	}
                
            
            }
        });

//        holder.btn_detail.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (mListener != null) {
//                    mListener.onDetail(item);
//                }
//            }
//        });

        return convertView;
    }

    class ViewHolder {
        ImageView img_blue;
        TextView txt_name;
        TextView txt_mac;
        TextView tv_connectstatus;
        Button btn_connect_operation;
        Button btn_detail;
    }

    public interface OnItemClickListener {
        void onConnect(Item item);

        void onDisConnect(Item item);

//        void onDetail(Item item);
    }

    private OnItemClickListener mListener;

    public void setOnDeviceClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public class Item{
    	private String name;
    	private String description;
    	private Object object;
    	
    	public Item(){}
    	public Item(String name,String description,Object object){
    		this.name = name;
    		this.description = description;
    		this.object = object;
    	}
    	public void setName(String name){
    		this.name = name;
    	}
    	public String getName(){
    		return name;
    	}
    	
    	public void setDescription(String description){
    		this.description = description;
    	}
    	public String getDescription(){
    		return description;
    	}
    	
    	public void setName(Object object){
    		this.object = object;
    	}
    	public Object getObject(){
    		return object;
    	}
    }
}

